﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using NHibernate.Util;
using NHibernate.Cfg.ConfigurationSchema;
using Environment = NHibernate.Cfg.Environment;


namespace FantastPlatform.Framework
{
    public class SessionFactory
    {
        //SingleTon,just create only one sessionFactory
        private static readonly ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();

        private static readonly string current_session_context_class =
            (System.Configuration.ConfigurationManager.GetSection("hibernate-configuration") as HibernateConfiguration)
            .SessionFactory.Properties["current_session_context_class"];

        /// <summary>
        /// create a Session
        /// </summary>
        /// <returns></returns>
        public static ISession GetSession()
        {
            if (current_session_context_class == "web")
            {
                return sessionFactory.GetCurrentSession();
            }

            return sessionFactory.OpenSession();

        }

        public static ISession CreateSession()
        {
            return sessionFactory.OpenSession();
        }

        private SessionFactory() { }

        public static ISessionFactory Instance
        {
            get { return sessionFactory; }
        }
    }
}
